package com.shuqing.dao;

import com.github.pagehelper.Page;
import com.shuqing.domain.Book;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**
 * @program: ShuqingLibrary
 * @ClassName BookMapper
 * @description:
 * @author: Ats
 * @create: 2025-03-31 14:43
 * @Version 1.0
 */
public interface BookMapper {

 @Select("select * from book where book_status != '3' ORDER BY book_uploadtime DESC")
 @Results(id="bookMap",value = {
         @Result(id = true, column = "book_id",property = "id"),
         @Result(column = "book_name",property = "name"),
         @Result(column = "book_isbn",property = "isbn"),
         @Result(column = "book_press",property = "press"),
         @Result(column = "book_author",property = "author"),
         @Result(column = "book_pagination",property = "pagination"),
         @Result(column = "book_price",property = "price"),
         @Result(column = "book_uploadtime",property = "uploadTime"),
         @Result(column = "book_status",property = "status"),
         @Result(column = "book_borrower",property = "borrower"),
         @Result(column = "book_borrowtime",property = "borrowTime"),
         @Result(column = "book_returntime",property = "returnTime")
 })
 Page<Book> selectNewbooks();


 @Select("select * from book where book_id = #{id}")
 @ResultMap("bookMap")
 Book findById(Integer id);


 Integer editBook(Book book);

@Select({"<script>"+
        "select * from book where book_status !='3' "+
        "<if test=\"name != null\">\n" +
        "            and book_name like concat('%',#{name},'%') \n" +
        "        </if>" +
        "<if test=\"press != null\">\n" +
        "           and book_press like concat('%',#{press},'%') \n" +
        "        </if>" +
        "<if test=\"author != null\">\n" +
        "           and book_author like concat('%',#{author},'%') \n" +
        "        </if>" +
        "order by book_borrowtime desc"+
        "</script>"
})
@ResultMap("bookMap")
Page<Book> searchBooks(Book book);

Integer addBook(Book book);

 /**
  * 查询已借阅但未归还的图书和所有待确认归还的图书 管理员的方法 比如 admin
  * @param book
  * @return
  */
 @Select({"<script>" +
        "select * from book " +
        "where book_borrower=#{borrower} " +
        "and book_status = '1' " +

        "<if test=\"name != null\">\n" +
        "            and book_name like concat('%',#{name},'%') \n" +
        "        </if>" +
        "<if test=\"press != null\">\n" +
        "           and book_press like concat('%',#{press},'%') \n" +
        "        </if>" +
        "<if test=\"author != null\">\n" +
        "           and book_author like concat('%',#{author},'%') \n" +
        "        </if>" +

        "or book_status = '2' " +

        "<if test=\"name != null\">\n" +
        "            and book_name like concat('%',#{name},'%') \n" +
        "        </if>" +
        "<if test=\"press != null\">\n" +
        "           and book_press like concat('%',#{press},'%') \n" +
        "        </if>" +
        "<if test=\"author != null\">\n" +
        "           and book_author like concat('%',#{author},'%') \n" +
        "        </if>" +

        "order by book_borrowtime desc " +
        "</script>"
})
@ResultMap("bookMap")
 Page<Book> selectBorrowed(Book book);

 /**
  * 查阅当前用户借阅但未归还的图书 普通用户的方法  比如zhangsan
  * @param book
  * @return
  */

@Select({"<script>" +
        "select * from book " +
        "where book_borrower=#{borrower} " +
        "and book_status in ('1','2') " +

        "<if test=\"name != null\">\n" +
        "            and book_name like concat('%',#{name},'%') \n" +
        "        </if>" +
        "<if test=\"press != null\">\n" +
        "           and book_press like concat('%',#{press},'%') \n" +
        "        </if>" +
        "<if test=\"author != null\">\n" +
        "           and book_author like concat('%',#{author},'%') \n" +
        "        </if>" +

        "order by book_borrowtime desc " +
"</script>"
})
@ResultMap("bookMap")
Page<Book> selectMyBorrowed(Book book);

}

